package org.algorithm.biubiu.wheel_2.链表;

import org.algorithm.biubiu.wheel_1.common.ListNode;

public class 两数相加 {

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        if(l1==null||l2==null){
            return l1==null?l2:l1;
        }
        int carry = 0;
        ListNode  result = new ListNode();
        ListNode  dummy = result;
        while( l1!=null || l2!=null ){
            int num=0;
            if(l1==null){
                num = l2.val+carry;
            }else if (l2 ==null){
                num = l1.val+carry;
            }else{
                num = l1.val+l2.val+carry;
            }
            carry = num/10;
            dummy.next = new ListNode(num%10);
            if(l1!=null) l1 = l1.next;
            if(l2!=null) l2 = l2.next;
            dummy = dummy.next;
        }


        if(carry!=0){
            dummy.next = new ListNode(carry);
        }
        return result.next;

    }
}
